package iyoga.check;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import tables.member.Member;
import tables.member.MemberDAO;

public class CardCheck {
	private MemberDAO mdao;
	
	public CardCheck()
	{
		mdao = new MemberDAO();
	}
	public boolean isNumeric(String str){ 
		   Pattern pattern = Pattern.compile("[0-9]*"); 
		   Matcher isNum = pattern.matcher(str);
		   if( !isNum.matches() ){
		       return false; 
		   } 
		   return true; 
	}
	/**
	* 功能：判断一个字符串是否包含特殊字符
	* @param string 要判断的字符串
	* @return true 提供的参数string不包含特殊字符
	* @return false 提供的参数string包含特殊字符
	*/
	private boolean isConSpeCharacters(String string) {
	   // TODO Auto-generated method stub
	   if(string.replaceAll("[\u4e00-\u9fa5]*[a-z]*[A-Z]*\\d*-*_*\\s*", "").length()==0){
	    //如果不包含特殊字符
	    return true;
	   }
	   return false;
	}
	public String checkName(String name)
	{
		int length = name.length();
		if(length > 25)
			return "用户名过长，中文25字符以内";
		if(!isConSpeCharacters(name))
			return "用户名包含特殊字符";
		String query2 = "from Member mem where mem.name = '" +name+"'";
		List<Member> memlist = mdao.query(query2);
		if(memlist.size() == 0)
		{
			return "用户名无效";   
		}
		return "ok"; 
	}
	public String checkTimes(int times)
	{
		if(times < 0)
			return "输入的次数为负数";
		return "ok";
	}
	public String checkType(String type)
	{
		if(type.equals("年卡") || type.equals("次卡"))
			return "ok";
		else
			return "卡类型错误，应为年卡或次卡";
		
	}

}
